// miniprogram/pages/word/home/home.js
Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的初始数据
   */
  data: {
    studied: 0,
    study: 0,
    reviewed: 0,
    review: 0,
    quote: {
      // date: "",
      // context: "",
      // author: "",
    },
    studyDays: 0,
    reviewDays: 0,
  },

  lifetimes: {

    attached: function(e) {
      // let thiz = this;
      if (this.data.study == 0) {
        this.setBatch()
        this.getCount()
        this.getQuote()
        this.getDays()
      }
    },

  },

  pageLifetimes: {
    show: function() {
      // 页面被展示
      this.setBatch()
      this.getCount()
      this.getQuote()
      this.getDays()

    },
    hide: function() {
      // 页面被隐藏
    },
    resize: function(size) {
      // 页面尺寸变化
    }
  },

  methods: {
    /**
     * 複製
     */
    copy(e) {
      console.log(e)
      let data = e.currentTarget.dataset.value
      wx.setClipboardData({
        data: data,
        success(res) {
          wx.getClipboardData({
            success(res) {
              console.log("成功选中复制内容", res.data) // data
            }
          })
        }
      })

    },


    /**
     * 从服务器拿今天已经背的单词数量
     */
    getCount() {
      let thiz = this;
      thiz.setData({
        loadModal: true
      })

      let count = 5

      wx.cloud.callFunction({
        // 要调用的云函数名称
        name: 'lastWord',
        // 传递给云函数的event参数
        data: {}
      }).then(res => {
        count = res.result.count
        console.log("callFunction lastWord res", res)
        let lastWordId = res.result.word_id

        let i = 0;
        numDH();

        function numDH() {
          if (i > 20) {
            // setTimeout(function() {
            //   thiz.setData({
            //     study: i,
            //     review: i,
            //   })
            //   i++
            //   numDH();
            // }, 1)
          } else {
            thiz.setData({
              studied: count,
              study: wx.getStorageSync("batch"),
            })

            let app = getApp()
            app.globalData.studiedToday = count
            app.globalData.lastWordId = lastWordId
          }
        }
        thiz.setData({
          loadModal: false
        })

      }).catch(err => {
        // handle error
        thiz.setData({
          loadModal: false
        })
      })

    },

    /**
     * 设置要背的单词数量和复习数量
     */
    setBatch() {
      if (!wx.getStorageSync("batch")) {
        wx.setStorageSync("batch", 25)
      }
      if (!wx.getStorageSync("reviewNumber")) {
        wx.setStorageSync("reviewNumber", 25)
      }
    },

    /**
     * 从服务器获取名人名言
     */
    getQuote() {
      const thiz = this

      wx.cloud.callFunction({
        // 要调用的云函数名称
        name: 'randomSentence',
        // 传递给云函数的参数
        data: {},
        success: res => {
          // output: res.result === 3
          let _ = {}
          _.date = res.result.date
          _.context = res.result.sentence
          _.author = res.result.author
          thiz.setData({
            quote: _
          })
          console.log("data.quote", thiz.data.quote)
        },
        fail: err => {
          // handle error
        },
        complete: () => {
          // ...
        }
      })

    },

    /**
     * 从服务器获取天数
     */
    getDays() {
      const thiz = this

      wx.cloud.callFunction({
        // 要调用的云函数名称
        name: 'getDays',
        // 传递给云函数的参数
        data: {},
        success: res => {
          // output: res.result === 3
          let studyDays = res.result.learnDays
          let reviewDays = res.result.reviewDays
          console.log("getDays", res)
          thiz.setData({
            studyDays: studyDays,
            reviewDays: reviewDays
          })
        },
        fail: err => {
          // handle error
        },
        complete: () => {
          // ...
        }
      })


    }

  }
})